<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<script>
	var form_url = $.webapp.root + "/admin/system/position/add.do" ; 
	$(function() {
		$(".append").click(function() {
			var tableform = $(".tableform") ;
			var tr = tableform.find("tr:last-child") ;
			tr.clone().appendTo(tableform);
			
			$(".remove").unbind("click");
			$(".remove").click(function() {
				if($(".tableform tr").length > 2) {
					$(this).parent().parent().remove();
				}
			});
		});
		
		
		//编辑，加载表单数据
		$.post("${ctx}/admin/system/quotient/list.do", {"positionId": "${positionId}"}, function(result) {
			if(null != result && result.length > 0) {
				var tf = $(".tableform") ;
				var oldTr = tf.find("tr:last-child") ;
				var tr = oldTr.clone();
				oldTr.remove();
				
				$.each(result, function(i, item){
					var _tr = tr ;
					_tr.find("input[name=year]").val(item.year);
					_tr.find("input[name=quotient]").val(item.quotient);
					_tr.clone().appendTo(tf) ;
				});
				$(".remove").unbind("click");
				$(".remove").click(function() {
					if($(".tableform tr").length > 2) {
						$(this).parent().parent().remove();
					}
				});
			}
		}, 'json').error(function(){$.easyui.loaded();});
	});
	
	//提交表单数据
	var submitNow = function($d, $tg, flag) {
		var params = [];
		var year = [];
		var years = $("#form [name=year]") ;
		var quotient = $("#form [name=quotient]") ;
		
		for(var i=0; i<years.length; i++) {
			if("" != $(years[i]).val() && "" != $(quotient[i]).val()) {
				year.push($(years[i]).val()) ;
				params.push({
					"year": $(years[i]).val(),
					"quotient": $(quotient[i]).val(),
					"positionId": "${positionId}"
				});
			}
		}
		//合并重复年的数据
		$.array.distinct(params, function(item1, item2) {
			return item1.year == item2.year;
		});
		
		if(params.length > 0) {
			$.ajax({  
	            type: "post",
	            url: "${ctx}/admin/system/quotient/add.do",
	            data:JSON.stringify(params),
	            contentType: "application/json",  
	            dataType: "json",  
	            success: function (result) {  
	            	if (result.status) {
	    				$tg.treegrid("reload");$.easyui.loaded(); 
	    				alertify.success(result.msg);
	    				if(flag==undefined||flag==false){$d.dialog("close") ;}
	    			} else {
	    				$.easyui.loaded();$.messager.alert("错误", result.msg, "error");
	    			}
	            }, error: function () {  
	            	$.easyui.loaded();
	            }  
	        });
		} else {
			$.easyui.loaded();
			$.messager.alert("警告", "请填写数据在提交！", "warning")
		}
	};
	
	//验证表单
	var submitForm = function($d, $tg, flag) {
		$.easyui.loading({ msg: "数据提交中，请稍等..." });
		submitNow($d, $tg, flag) ;
	};
</script>

<form id="form" class="easyui-form form_container">
	<input type="hidden" name="id" value="${id}" />
	<input type="hidden" name="modifyName" value="${USER_SESSION.user.emp_name}" />
	<table class="tableform">
		<tr>
			<th colspan="6">
				<a href="javascript:void(0);" class="easyui-linkbutton append" data-options="plain: true, iconCls: 'ext_add'">追加</a>
			</th>
		</tr>
		<tr>
			<th>年份：</th>
			<td><input name="year" class="easyui-validatebox" type="text" data-options="prompt: '年份'" style="width:50px;"/></td>
			<th>系数：</th>
			<td><input name="quotient" class="easyui-validatebox" type="text" data-options="prompt: '系数'"  style="width:50px;"/></td>
			<td style="text-align: center;">
				<a href="javascript:void(0);" class="easyui-linkbutton remove" data-options="plain: true, iconCls: 'ext_cancel'"></a>
			</td>
		</tr>
	</table>
</form>